home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / read / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-16  |  4.3 KB  |  182 lines

  1. #include "sprite.h"
  2. #include "time.h"
  3. #include "fs.h"
  4. #include "stdio.h"
  5. #include "option.h"
  6.  
  7. static char *buffer;
  8.  
  9. int    repeats = 1;
  10. int    blockSize = 16384;
  11. int    msToPause = -1;
  12. int    loops = -1;
  13. Boolean errorTest = FALSE;
  14.  
  15. Option optionArray[] = {
  16.     {OPT_INT, "r", (Address) &repeats,
  17.      "\tNumber of times to repeat read (Default 1)."},
  18.     {OPT_INT, "b", (Address) &blockSize, 
  19.      "\tBlock size to use for reading (Default 16384)."},
  20.     {OPT_INT, "p", (Address)&msToPause,
  21.      "\tMilliseconds to pause between reads of each block. "},
  22.     {OPT_INT, "l", (Address)&loops,
  23.      "\tNumber of times to loop between reads of each block. "},
  24.     {OPT_TRUE, "e", (Address)&errorTest,
  25.      "\tTest error cases. "},
  26. };
  27. int numOptions = sizeof(optionArray) / sizeof(Option);
  28.  
  29. int Handler();
  30. int gotSig = FALSE;
  31.  
  32. main(argc, argv)
  33. int argc;
  34. char **argv;
  35. {
  36.     int         cnt, total;
  37.     double         rate, tmp;
  38.     Time         before, after;
  39.     int            newOffset;
  40.     int        status;
  41.     Time        pauseTime;
  42.     Sig_Action        newAction, oldAction;
  43.     register    int    i;
  44.  
  45.     argc = Opt_Parse(argc, argv, optionArray, numOptions, 0);
  46.  
  47.     /*
  48.      * Set up signal handling, trap interrupts in order to test
  49.      * the GEN_INTERRUPTED_BY_SIGNAL return code.
  50.      */
  51.     newAction.action = SIG_HANDLE_ACTION;
  52.     newAction.handler = Handler;
  53.     newAction.sigHoldMask = 0;
  54.     Sig_SetAction(SIG_INTERRUPT, &newAction, &oldAction);
  55.  
  56.     buffer = (char *)malloc(blockSize);
  57.     total = 0;
  58.     if (msToPause > 0) {
  59.     pauseTime.seconds = 0;
  60.     pauseTime.microseconds = msToPause * 1000;
  61.     }
  62.  
  63.     if (errorTest) {
  64.     int numErrors = 0;
  65.     printf("Read Error Tests NOT IMPLEMENTED\n"); 
  66.  
  67. #ifdef notdef
  68.     Fs_Write(1, 3, "? ", &cnt);
  69.     status = Fs_Read(-2, 0, 0, &cnt);
  70.     if (status == SUCCESS) {
  71.         printf("ERROR: Fs_Read(-2) worked!\n");
  72.         numErrors++;
  73.     } else {
  74.         Stat_PrintMsg(status, "Fs_Read(-2)");
  75.     }
  76.  
  77.     Fs_Write(1, 3, "? ", &cnt);
  78.     status = Fs_Read(0, 10, -1, &cnt);
  79.     if (status == SUCCESS) {
  80.         printf("ERROR: Fs_Read{buffer = -1} worked!\n");
  81.         numErrors++;
  82.     } else {
  83.         Stat_PrintMsg(status, "Fs_Read{buffer = -1}");
  84.     }
  85.  
  86.     Fs_Write(1, 3, "? ", &cnt);
  87.     status = Fs_Read(0, -1, buffer, &cnt);
  88.     if (status == SUCCESS) {
  89.         printf("ERROR: Fs_Read{count < 0} worked!\n");
  90.         numErrors++;
  91.     } else {
  92.         Stat_PrintMsg(status, "Fs_Read{count < 0}");
  93.     }
  94.  
  95.     /*
  96.      * This uses Fs_RawRead because the library routine Fs_Read
  97.      * dies on a bad amountReadPtr.
  98.      */
  99.     Fs_Write(1, 3, "? ", &cnt);
  100.     status = Fs_RawRead(0, 10, buffer, 0);
  101.     if (status == SUCCESS) {
  102.         printf("ERROR: Fs_RawRead{&cnt = 0} worked!\n");
  103.         numErrors++;
  104.     } else {
  105.         Stat_PrintMsg(status, "Fs_RawRead{&cnt = 0}");
  106.     }
  107.  
  108.     {
  109.         int outFD2;
  110.         status = Fs_Open("/dev/null", FS_WRITE, 0,&outFD2);
  111.         if (status != SUCCESS) {
  112.         fprintf(stderr, "Could not open %s for writing, status %x\n",
  113.                    "/dev/null", status);
  114.         } else {
  115.         status = Fs_Read(outFD2, 10, buffer, &cnt);
  116.         if (status == SUCCESS) {
  117.             printf("ERROR: Fs_Read{writeonly stream} worked!\n");
  118.             numErrors++;
  119.         } else {
  120.             Stat_PrintMsg(status, "Fs_Read{writeonly stream}");
  121.         }
  122.         }
  123.     }
  124.  
  125.     {
  126.         char *newBuf = (char *)malloc(100 * 1024);
  127.         printf("Starting 100K read... "); Io_Flush(io_StdOut);
  128.         status = Fs_RawRead(0, 100 * 1024, newBuf, &cnt);
  129.         if (gotSig) {
  130.         printf("Got Signal, "); Io_Flush(io_StdOut);
  131.         }
  132.         if (status == SUCCESS) {
  133.         printf("Read %d bytes\n", cnt);
  134.         } else {
  135.         Stat_PrintMsg(status, "read");
  136.         }
  137.     }
  138.  
  139.     Fs_Close(0);
  140.     Fs_Write(1, 3, "? ", &cnt);
  141.     status = Fs_Read(0, 10, buffer, &cnt);
  142.     if (status == SUCCESS) {
  143.         printf("ERROR: Fs_Read{closed stream} worked!\n");
  144.         numErrors++;
  145.     } else {
  146.         Stat_PrintMsg(status, "Fs_Read{closed stream}");
  147.     }
  148.     if (numErrors) {
  149.         printf("Read Test had %d errors\n", numErrors);
  150.     } else {
  151.         printf("No errors\n");
  152.     }
  153. #endif
  154.     exit(numErrors);
  155.     } else {
  156.     Sys_GetTimeOfDay(&before, NULL, NULL);
  157.     for ( ; repeats > 0; repeats--) {
  158.         lseek(0, 0, 0);
  159.         while (1) {
  160.         if (loops > 0) {
  161.             for (i = loops; i > 0; i --) {
  162.             }
  163.         }
  164.         cnt = read(0, buffer, blockSize, buffer);
  165.         total += cnt;
  166.         if (cnt < blockSize) break;
  167.         }
  168.     }
  169.     Sys_GetTimeOfDay(&after, NULL, NULL);
  170.     rate = after.seconds - before.seconds;
  171.     rate += (after.microseconds - before.microseconds)*.000001;
  172.     rate = total/rate;
  173.     printf("%d bytes read at %.0f bytes/sec.\n", total, rate);
  174.     }
  175. }
  176.  
  177. int
  178. Handler()
  179. {
  180.     gotSig = TRUE;
  181. }
  182.